﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections;

namespace Data.Database
{
    public class Box
    {
        static private String cadenaConexion = ConfigurationManager.ConnectionStrings["Turnet"].ConnectionString; // @"Data Source=|DataDirectory|\BBDD.sdf";
        static private String nombreTabla = "Box";

        public DataSet ObtenerTablaBox()
        {
            DataSet dsBox = new DataSet();

            try
            {
                SqlConnection conexion = new SqlConnection(cadenaConexion);
                String consulta = "SELECT * FROM Box";
                SqlDataAdapter daBox = new SqlDataAdapter(consulta, conexion);
                daBox.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                daBox.Fill(dsBox, nombreTabla);
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return dsBox;
        }

        public void AnyadirBox(DataSet box)
        {
            DataSet dsBox = new DataSet();
            try
            {
                SqlConnection conexion = new SqlConnection(cadenaConexion);
                String consulta = "Select * from Box";
                SqlDataAdapter daBox = new SqlDataAdapter(consulta, conexion);
                daBox.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                daBox.Fill(dsBox, nombreTabla); //nombre de tabla esta declarado arriba del todo y sera "Especialidad"
                SqlCommandBuilder cbuilder = new SqlCommandBuilder(daBox);
                daBox.Update(box, "Box");

            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public DataSet ObtenerDatosBox(string boxVieja)
        {
            DataSet dsBox = new DataSet();

            try
            {
                SqlConnection conexion = new SqlConnection(cadenaConexion);
                String consulta = "SELECT * FROM Box WHERE Nombre ='" + boxVieja + "'";
                SqlDataAdapter daBox = new SqlDataAdapter(consulta, conexion);
                daBox.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                daBox.Fill(dsBox, nombreTabla);
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return dsBox;
        }

        public void EditarBox(DataSet box)
        {
            DataSet dsBox = new DataSet();
            try
            {

                SqlConnection conexion = new SqlConnection(cadenaConexion);

                String consulta = "Select * from Box where IdBox ='" + box.Tables["Box"].Rows[0][0] + "'";
                SqlDataAdapter daBox = new SqlDataAdapter(consulta, conexion);
                daBox.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                daBox.Fill(dsBox, nombreTabla);
                SqlCommandBuilder cbuilder = new SqlCommandBuilder(daBox);
                daBox.Update(box, nombreTabla);

            }
            catch (Exception ex)
            {
                Exception excepcionManejada = new Exception("Error al modificar datos del Box", ex);
                throw ex;
            }
        }
    }
}
